package com.junxonline.instant.dao.entity.vo.iobean;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.regex.Pattern;

/**
 * @author Virgo
 * @version 1.0
 * @data 2022/6/1 9:31
 * @usage ecxel 导入导出涉及工具类
 */
public class ExcelUtils {
    public static final String FORMAT_YMD = "yyyy-MM-dd";//10
    public static final String FORMAT_YMDHM = "yyyy-MM-dd HH:mm";//16
    public static final String FORMAT_YMDHMS = "yyyy-MM-dd HH:mm:ss";//16
    public static final String FORMAT_HMS = "HH:mm:ss";//8
    public static final String FORMAT_HM = "HH:mm";//5

    /**
     * Sat Dec 30 1899 00:00:15 GMT+0805 (中国标准时间)
     * 此格式日期无法传到java后台 ==> 1899-12-29T15:54:32.000Z
     * 中国标准时间转换
     *
     * @param time string 中国标准时间
     * @return Date
     **/
    public static Date ChineConvert(String time) {
        //1899-12-29T15:54:32.000Z
        System.out.println(">>>" + time);
        try {
            time = time.split(Pattern.quote("(中国标准时间)"))[0].replace("GMT+0805", "GMT+08:00");
            SimpleDateFormat sdf = new SimpleDateFormat("E MMM dd yyyy HH:mm:ss z", Locale.US);
            return sdf.parse(time);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * Sat Dec 30 1899 00:00:15 GMT+0805 (中国标准时间)
     * 中国标准时间转换
     *
     * @param time   string 中国标准时间
     * @param format 输出的时间格式
     * @return string
     **/
    public static String ChineConvert(String time, String format) {
        Date date = ChineConvert(time);
        if (date == null) {
            return null;
        } else {
            return DateUtil.format(date, format);
        }
    }


    /**
     * String 转 Date
     */
    public static Date getDate(String date, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        Date parse = null;
        if (date != null) {
            try {
                parse = sdf.parse(date);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return parse;
    }

    /**
     * 格式化时长 int s -> string hh:mm:ss
     *
     * @param duration int
     * @return string
     */
    public static String formatDuration(int duration) {//s
        String time;
        int minute;
        int second;
        if (duration > 3600) {
            int hour = duration / 3600;
            minute = (duration % 3600) / 60;
            second = duration - hour * 3600 - minute * 60;
            time = unitFormat(hour) + ":" + unitFormat(minute) + ":" + unitFormat(second);
        } else if (duration > 60) {
            minute = duration / 60;
            second = duration % 60;
            time = "00:" + unitFormat(minute) + ":" + unitFormat(second);
        } else {
            time = "00:00:" + unitFormat(duration);
        }
        return time;
    }

    private static String unitFormat(int i) {
        if (i >= 0 && i < 10)
            return "0" + (i);
        else {
            return String.valueOf(i);
        }
    }

    /**
     * 格式化存储单位
     *
     * @param size KB
     * @return
     */
    public static String formatSize(long size) {
        int gb = 1024 * 1024;//GB
        int mb = 1024;//MB

        if (size < mb) {
            return size + " KB";
        } else {
            if (size >= gb) {
                return String.format("%.2f GB", (size / gb * 1f));
            } else {
                return String.format("%.2f MB", (size / mb * 1f));
            }
        }
    }

    /**
     * 截取yyyy-MM-dd HH:mm:ss 后半段
     * @return
     */
    public static String parseTime(String time) {
        if (StrUtil.isEmpty(time)) {
            return null;
        }

        if (time.contains(" ")) {//yyyy-MM-dd HH:mm:ss
            time = time.split(" ")[1];
        }

        return time;
    }
}
